import os

import win32com.client
from docx import Document


def doc2docx(path):
    w = win32com.client.Dispatch('Word.Application')
    w.Visible = True
    w.DisplayAlerts = 0
    doc = w.Documents.Open(path)
    newpath = os.path.splitext(path)[0] + '.docx'
    doc.SaveAs(newpath, 12, False, "", True, "", False, False, False, False)
    doc.Close()
    w.Quit()
    os.remove(path)
    return newpath


word_dir = r"F:\xun\学生毕设"
word_files = os.listdir(word_dir)
for word_name in word_files:
    word_file = os.path.join(word_dir, word_name)
    if not os.path.isfile(word_file) or not os.path.exists(word_file):
        continue

    print(word_file)
    if word_file.endswith(".doc"):
        word_file = doc2docx(word_file)

    doc = Document(word_file)
    for p in doc.paragraphs:
        style_name = p.style.name
        if p.style.name.startswith('Heading'):
            print(style_name, p.text, sep=':')
